<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.8/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset" style="width: 560px;">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">标签</label>
                            <div class="layui-input-inline">
                                <input type="text" name="like_with_name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>
<script src="/lib/layui-v2.6.8/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            table = layui.table;

        $.get('/admin/user/getCurrent', {}, function (re) {
            if (re.code == 0) {
                layui.use('watermark', function (watermark) {
                    watermark({watermark_txt: re.data.name});
                });
            }
        }, 'json');

        layui.delOne = function (id) {
            layer.confirm('真的删除么？', function (index) {
                $.post('/admin/tag/delete', {'id': id}, function (re) {
                    if (re.code == 0) {
                        table.reload('currentTableId');
                    } else {
                        layer.msg('删除失败, ' + re.msg);
                    }
                }, 'json');
                layer.close(index);
            });
        };

        table.render({
            elem: '#currentTableId',
            url: '/admin/tag/list',
            where: {
                order: 'id desc'
            },
            done: function () {
                $('body, html').animate({
                    scrollTop: 0
                }, 100);
            },
            cols: [[
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'name', title: '<i class="layui-icon layui-icon-edit"></i>标签 (单击可编辑保存)', edit:'text'},
                {field: 'created_at', title: '创建时间', width: 160},
                {title: '删除', width: 90, templet: function (row) {
                    return '<button type="button" class="layui-btn layui-btn-danger layui-btn-xs" onclick="javascript:layui.delOne(' + row.id + ');"><i class="layui-icon layui-icon-delete"></i>删除</button>';
                }}
            ]],
            limits: [50, 100],
            limit: 50,
            page: true,
            skin: 'line',
            width: 600
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: {curr: 1},
                where: data.field
            }, 'data');
            return false;
        });

        // 监听单元格编辑
        table.on('edit(currentTableFilter)', function(obj) {
            let field = obj.field;
            let id = obj.data.id;
            let newValue = obj.value.trim();
            if (newValue == '' || id <= 0 || field != 'name') {
                layer.msg('内容错误！');
                table.reload('currentTableId');
                return false;
            }
            $.post('/admin/tag/update', {'id': id, 'name': newValue}, function (re) {
                let msg = '保存' + (re.code == 0 ? '成功' : '失败');
                layer.msg(msg);
            }, 'json');
        });

    });
</script>

</body>
</html>